package com.hbzhit.common.xss;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.HashMap;
import java.util.Map;

/**
 * Filter配置
 *
 * @author DING
 * @version 1.0
 * @date 2021/6/23 11:22
 */
@Configuration
public class XssFilterConfig {
    @Autowired
    private XssFilterProperties xssFilterProperties;
    
    @Bean
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new XssFilter());
        //添加过滤路径
        registration.addUrlPatterns(StrUtil.split(xssFilterProperties.getUrlPatterns(), ","));
        registration.setName("xssFilter");
        registration.setOrder(Integer.MAX_VALUE);
        //设置初始化参数
        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("excludes", xssFilterProperties.getExcludes());
        initParameters.put("enabled", String.valueOf(xssFilterProperties.isEnabled()));
        registration.setInitParameters(initParameters);
        return registration;
    }
}
